package blancosMikel.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import blancosMikel.controller.beans.ventasBeans;

public class VentasDAO extends SigaabmDAO{
	
	
	public void insertaVenta(ventasBeans bean) throws SQLException{
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();
			query.append("INSERT INTO ").append("ventas")
			
			.append(" VALUES(").append(bean.getid_ventas())
			.append(",").append(bean.getId_usuario())
			.append(",'").append(bean.getFecha()).append("'")
			.append(",").append(bean.getSubtotal())
			.append(")").append(";");
			conn.prepareStatement(query.toString());
			ps.executeUpdate();
			rs.close(); 
			ps.close();
			conn.close();
	    }catch (SQLException sql) {	    	
	    } finally{
			rs.close();
			ps.close();
			conn.close();
		}
	}
	
	public ArrayList<ventasBeans> getUsuarioParaVenta(char tipo) throws SQLException {
		ArrayList<ventasBeans> lista= new ArrayList<ventasBeans>();
		try {
			StringBuilder query = new StringBuilder();
			query.append("Select * from ventas where tipoDeVenta = '"+ tipo +"'; ");
                      
			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			System.out.println("dao.usuarios.getCatalogo.Ejecutado----------> "
					+ query.toString());

			while (rs.next()) {
				ventasBeans bean = new ventasBeans();
				bean.setid_ventas(rs.getInt(0));
				bean.setId_usuario(rs.getInt(1));
				bean.setFecha(rs.getString(2));
				bean.setSubtotal(rs.getDouble(3));
				bean.setTipoDeVenta(tipo);
				lista.add(bean);
			}
			System.out.println("dao.usuarios.getCatalogo----------> Cargado");
		} catch (Exception e) {
			System.out.println("usuariosDao...Error..." + e.getMessage());
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return lista;
	}
	
	public void eliminaVenta(int id) throws SQLException{
		StringBuilder query = new StringBuilder();
		try {
			conn = this.getConnectionMYSQL();
			query.append("DELETE FROM ").append("ventas")
			.append(" WHERE id_ventas =").append(id).append(";");
			conn.prepareStatement(query.toString());
			ps.executeUpdate();
			rs.close();
			ps.close();
			conn.close();
	    }catch (SQLException sql) {	    	
	   } finally{
			rs.close();
			ps.close();
			conn.close();
		}
	}
}
